package com.yuanfeng.controller;

import com.yuanfeng.basedata.annotation.ConfigManagementSwagger;
import com.yuanfeng.basedata.api.FineFoodConstantApi;
import com.yuanfeng.basedata.entiry.KcsjUser;
import com.yuanfeng.basedata.response.ResponseResult;
import com.yuanfeng.basedata.utils.AesEncryptUtil;
import com.yuanfeng.service.UserManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @ClassName UserManageController
 * @Description T0D0
 * @Author yuanfeng
 * @Date 2019/9/7 10:14
 * @Version 1.0
 **/
@RequestMapping(FineFoodConstantApi.ADMIN_PATH)
@RestController
@Validated
@Api(description = "后台用户管理模块")
@ConfigManagementSwagger
public class UserManageController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserManageController.class);


    @Value("${aes_key}")
    private String aesKey;
    @Value("${aes_iv}")
    private String aesIv;

    @Autowired
    private UserManageService userManageService;

    @ApiOperation(value = "新增用户")
    @RequestMapping(value = "/users/add", method = RequestMethod.POST)
    public ResponseResult<Map<String, Object>> addUsers(@ApiParam("用户实体") @RequestBody KcsjUser kcsjUser) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        try {

            String password = AesEncryptUtil.encrypt(kcsjUser.getUserPassword(), aesKey, aesIv).trim();
            kcsjUser.setUserPassword(password);
            KcsjUser addUser = userManageService.addUser(kcsjUser);

            //新增成功后不为空
            if (Objects.nonNull(addUser)) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("result", addUser);
                responseResult.setCode(200);
                responseResult.setMessage("用户新增成功!");
                responseResult.setData(resultMap);
            }

        } catch (Exception e) {
            LOGGER.error(e + "");
            responseResult.setCode(500);
            responseResult.setMessage("用户新增失败!");
            responseResult.setData(null);
        }
        return responseResult;
    }

    @ApiOperation(value = "根据ID删除用户")
    @RequestMapping(value = "/delete/user", method = RequestMethod.DELETE)
    public ResponseResult<Map<String, Object>> userColumns(@ApiParam("用户ID") @RequestParam String userId) {

        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        try {
            int paramId = Integer.parseInt(userId);
            KcsjUser kcsjUser = userManageService.deleteUser(paramId);
            if (Objects.nonNull(kcsjUser)) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("result", kcsjUser);
                responseResult.setCode(200);
                responseResult.setMessage("用户删除成功!");
                responseResult.setData(resultMap);
            }
        } catch (Exception e) {
            LOGGER.error(e + "");
            responseResult.setCode(500);
            responseResult.setMessage("用户删除失败!");
            responseResult.setData(null);
        }
        return responseResult;
    }

}
